﻿using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
using static DataCapture.Helper.PublicHelp;
using System.Drawing;
using System;
using System.Runtime.InteropServices;
using DataCapture.Helper;
using System.Collections.Concurrent;


namespace DataCapture.DeviceDataSave;

public abstract class DeviceDataCaptureBase
{
    protected ElementConfig elementConfig;

    protected readonly ConcurrentDictionary<string, Delegate> _cachedDelegates;
    protected readonly ConcurrentDictionary<string, int> _plcData ;//动作数据缓存包含（气缸、轴、IO数据）
    protected readonly ConcurrentDictionary<string , AlarmData> AlarmDatas;    //告警数据缓存
    protected readonly ConcurrentDictionary<string , AlarmAddressConfig> AlarmConfig;    //告警配置表缓存
    protected List<AlarmData> AlarmMqDatas;//   mqtt告警数据缓存(为true时发送mqtt消息)
    public PublicHelp.DeviceType deviceType { get; set; }// 设备类型
    public string plcName { get; set; }// plc名称
    public string deviceId { get; set; }// 设备Id
    public string topic { get; set; }// Topic
    public abstract  void  ReadActionData(CancellationToken cancellationToken,Action callAction);// 读取动作数据
    public abstract  void  ReadAlarmData(CancellationToken cancellationToken,Action callAction);// 读取告警数据
    public abstract  void  ReadTriggerData(CancellationToken cancellationToken,Action callAction);// 读取感应器、阻挡器数据
    

    public abstract void DataParserCylinder<T>(T ReadData, ModuleConfig plcPart);// 解析数据 气缸
    public abstract void DataParserAxle<T>(T ReadData);// 解析数据 轴
    // 解析数据 IOCOM如X、Y开头
    public virtual void DataParserIOCOM<T>(T ReadData)
    { 
    }
    // 解析数据 触发器（感应器、阻挡器）
    public virtual void DataParserTrigger<T>(T ReadData)
    {
    }
    public DeviceDataCaptureBase(ElementConfig elementConfig)
    {
        _cachedDelegates = new ConcurrentDictionary<string, Delegate>();
        _plcData=new ConcurrentDictionary<string, int>();
        AlarmDatas =new ConcurrentDictionary<string, AlarmData>();
        AlarmConfig=new ConcurrentDictionary<string, AlarmAddressConfig>();
        AlarmMqDatas =new List<AlarmData>();
        this.elementConfig = elementConfig;
        topic = elementConfig.Topic;
        deviceId=elementConfig.DeviceId;
        if (Enum.TryParse(elementConfig.DeviceId.Split('_')[0], out PublicHelp.DeviceType devType))
        {
            deviceType = devType;

        }
        else
        {
            Log4NetHelper.Debug($"{elementConfig.DeviceId}没有找到设备类型：{devType.ToString()}",true);
        }
    }
    /// <summary>
    /// 添加动作数据
    /// </summary>
    /// <param name="address"></param>
    /// <param name="value"></param>
    public void AddData(string address, int value)
    {
        try
        {
            //Log4NetHelper.Debug($"{address}:{value}", true);

            _plcData[address] = value;
        }
        catch (Exception e)
        {
            Log4NetHelper.Debug($"AddData异常：{e}", true);
            throw;
        }
    }

    /// <summary>
    /// 获取动作数据
    /// </summary>
    /// <param name="address"></param>
    /// <returns></returns>
    public int GetData(string address)
    {
        //Console.WriteLine($"GetData: {address}");
        return _plcData.TryGetValue(address, out var value) ? value : 0;
    }
}